using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Commerce.Common;
using umbraco.cms.businesslogic.media;

namespace CommerceForUmbraco.usercontrols
{
    public partial class FeaturedProductBox : System.Web.UI.UserControl
    {
        protected int productID;
        protected string ProductName;
        protected string ShortDescription;
        protected decimal RetailPrice;
        protected decimal OurPrice;
        protected string ImageFile = "";
        protected string productSku;
        protected Guid productGUID;
        protected string productUrl = "";

        private Commerce.Common.Product product = null;

        #region properties
        /// <summary>
        /// umbraco property - set in Macro
        /// </summary>
        private int _umbProductId;

        public int umbProductId
        {
            get { return _umbProductId; }
            set { _umbProductId = value; }
        }
        #endregion

        /// <summary>
        /// Handles the Load event of the FeatureProductBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (_umbProductId != 0)
                {
                    product = ProductController.GetProductDeep(_umbProductId);
                }

                //make sure we have a product
                TestCondition.IsTrue(product.IsLoaded, "Invalid url/product id");

                //set the page variables
                productGUID = product.ProductGUID;
                productID = product.ProductID;
                productSku = product.Sku;
                ProductName = product.ProductName;
                RetailPrice = product.RetailPrice;
                OurPrice = product.OurPrice;
                // get the image URL from the Umbraco Node id
                Media m = new Media(int.Parse(product.DefaultImage));
                ImageFile = m.getProperty("umbracoFile").Value.ToString();
                ShortDescription = product.ShortDescription;
                // set the product Url to a default value
                productUrl = "/product/" + productGUID.ToString() + ".aspx";
            }
            catch (Exception)
            {
                // set safe display

            }

        }

        /// <summary>
        /// Handles the Click event of the AddToCart button.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void AddToCart_Click(object sender, EventArgs e)
        {
            Commerce.Common.Product prod = new Commerce.Common.Product(productID);
            prod.Quantity = 1;
            OrderController.AddItem(prod);

            if (DontRedirectToCart)
            {
                CartEvent.Add(productID, 1);

            }
            else
            {
                // send to cart page
                Response.Redirect(Utility.GetCartUrl(), true);
            }

        }

        private bool _DontRedirectToCart;

        public bool DontRedirectToCart
        {
            get
            {
                return _DontRedirectToCart;
            }
            set
            {
                _DontRedirectToCart = value;
            }
        }
    }
}